/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.app.ad.domain.AduserXMovie;
import me.zhengjie.modules.app.ad.service.AduserXHotelService;
import me.zhengjie.modules.app.ad.service.AduserXMovieService;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelQueryCriteria;
import me.zhengjie.modules.app.ad.service.dto.AduserXMovieDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXMovieQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author HaojieLi
* @date 2021-03-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "广告商开机视频管理")
@RequestMapping("/api/aduserXMovie")
public class AduserXMovieController {

    private final AduserXMovieService aduserXMovieService;
    private final AduserXHotelService aduserXHotelService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('aduserXMovie:list')")
    public void download(HttpServletResponse response, AduserXMovieQueryCriteria criteria) throws IOException {
        aduserXMovieService.download(aduserXMovieService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询广告商开机视频")
    @ApiOperation("查询广告商开机视频")
    @PreAuthorize("@el.check('aduserXMovie:list')")
    public ResponseEntity<Object> query(AduserXMovieQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(aduserXMovieService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    @GetMapping("findByHotelId")
    @Log("查询广告商开机视频")
    @ApiOperation("查询广告商开机视频")
    @PreAuthorize("@el.check('aduserXMovie:list')")
    public ResponseEntity<Object> findByHotelId(AduserXHotelQueryCriteria criteria, Pageable pageable){
    	List<AduserXHotelDto> aduserXHotelDtos = aduserXHotelService.queryAll(criteria);
    	List<AduserXMovieDto> aduserXMovieDtos = null;
    	AduserXMovieQueryCriteria aduserXMovieQueryCriteria = new AduserXMovieQueryCriteria();
    	if(aduserXHotelDtos != null && aduserXHotelDtos.size() > 0) {
        	aduserXMovieQueryCriteria.setAdUserId(aduserXHotelDtos.get(0).getUserId());
        	aduserXMovieDtos = aduserXMovieService.queryAll(aduserXMovieQueryCriteria);
    	}
    	
    	if(aduserXMovieDtos == null ) {
    		JSONObject jsonObject = new JSONObject();
        	jsonObject.put("content", new AduserXMovieDto());
        	jsonObject.put("message", "未获取到相关数据");
    		return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    	}
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.put("content", aduserXMovieDtos.get(0));
    	jsonObject.put("message", "获取成功");
        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增广告商开机视频")
    @ApiOperation("新增广告商开机视频")
    @PreAuthorize("@el.check('aduserXMovie:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AduserXMovie resources){
        return new ResponseEntity<>(aduserXMovieService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改广告商开机视频")
    @ApiOperation("修改广告商开机视频")
    @PreAuthorize("@el.check('aduserXMovie:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AduserXMovie resources){
        aduserXMovieService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除广告商开机视频")
    @ApiOperation("删除广告商开机视频")
    @PreAuthorize("@el.check('aduserXMovie:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        aduserXMovieService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}